package display;


import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;

import javax.swing.JPanel;

import universe.BlockHandler;

@SuppressWarnings("serial")
public class BlockDisplay extends JPanel {

	//private Game theGame;
	private int theX = -1;
	private int theY = -1;

	private Container theContainer;
	private BlockHandler theBlockHandler;

	public BlockDisplay(Container pContainer, BlockHandler pBlockHandler){
		super();
		theContainer = pContainer;
		theBlockHandler = pBlockHandler;
		this.setSize(display.Utils.getBoardWidthPx()+Utils.paddingFrame, display.Utils.getBoardHeightPx()+Utils.paddingFrame);

		// Fond transparent
		this.setOpaque(false);
		theContainer.add(this);

		//System.out.println("allo les gens :" + display.Utils.getBlockWidthPx() +" " + display.Utils.getBlockHeightPx());
	}

	public BlockDisplay(Container pContainer, BlockHandler pBlockHandler, int pXpx , int pYpx){
		this(pContainer, pBlockHandler);
		theX = pXpx;
		theY = pYpx;
	}



	public void paintComponent(Graphics g){
		//System.out.println("on draw le block");
		// theBlockHandler = theGame.getCurrentBlockHandler();

		if(theBlockHandler.isVisible()){
			int posX = theBlockHandler.getX();
			int posY = theBlockHandler.getY();

			if(theX != -1 && theY != -1){
				posX = theX;
				posY = theY;
			}

			// Affichage de son clone
			if(Utils.DISPLAY_CLONE){
				int posXClone = posX;
				int posYClone = theBlockHandler.getYClone();
				Color blockColor = null;


				for(int i=0; i<universe.Utils.getBlockWidth();i++){
					for(int j=0; j<universe.Utils.getBlockHeight();j++){
						if(theBlockHandler.getB().getCell(i, j) != 0){

							//Color color = new Color((float)display.Utils.getCloneColor().getRed(),(float)display.Utils.getCloneColor().getGreen(),(float)display.Utils.getCloneColor().getBlue(),(float)0.5);
							if(blockColor == null){
								blockColor = Utils.getColor(theBlockHandler.getB().getCell(i, j));
								Color color = new Color(blockColor.getRed(),blockColor.getGreen(),blockColor.getBlue(),display.Utils.cloneAlpha);
								g.setColor(color);
							}
							g.fill3DRect(Utils.paddingFrame+(posXClone+i)*Utils.sideWidth, Utils.paddingFrame+(posYClone+j)*Utils.sideWidth, Utils.sideWidth, Utils.sideWidth,true);					
						}
					}
				}
			}


			// Affichage de la pièce en cours
			for(int i=0; i<universe.Utils.getBlockWidth();i++){
				for(int j=0; j<universe.Utils.getBlockHeight();j++){
					if(theBlockHandler.getB().getCell(i, j) != 0){
						g.setColor(Utils.getColor(theBlockHandler.getB().getCell(i, j)));
						g.fill3DRect(Utils.paddingFrame+(posX+i)*Utils.sideWidth, Utils.paddingFrame+(posY+j)*Utils.sideWidth, Utils.sideWidth, Utils.sideWidth,true);					
					}
				}
			}

		}
	}


	public BlockHandler getBlockHandler() {
		return theBlockHandler;
	}


	public void setBlockHandler(BlockHandler pBlockHandler) {
		this.theBlockHandler = pBlockHandler;
	}

	public void clear(){
		theContainer.remove(this);
	}
}
